home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS04.ADF / C / serialtest.c < prev    next >
C/C++ Source or Header  |  1986-01-07  |  4KB  |  161 lines

  1.  
  2. /* serial test program v1.1 */
  3.  
  4. #include        "exec/types.h"
  5. #include        "exec/nodes.h"
  6. #include        "exec/lists.h"
  7. #include        "exec/ports.h"
  8. #include        "exec/libraries.h"
  9. #include        "exec/devices.h"
  10. #include        "exec/io.h"
  11. #include        "devices/serial.h"
  12.  
  13. struct IOExtSer IORser;
  14. struct MsgPort *port;
  15. char   buffer[200];
  16. extern struct MsgPort *CreatePort();
  17.  
  18. main()
  19. {
  20.     int     error;
  21.     int     actual;
  22.     unsigned long rbl;
  23.     unsigned long brk;
  24.     unsigned long baud;
  25.     unsigned char rwl;
  26.     unsigned char wwl;
  27.     unsigned char sf;
  28.     unsigned long t0;
  29.     unsigned long t1;
  30.  
  31. open:
  32.  /* OPEN the serial.device */
  33.     if ((error = OpenDevice (SERIALNAME, 0, &IORser, 0)) != 0) {
  34.         printf ("bad news %ld on Open \n", error);
  35.         exit (error);
  36.     }
  37.  
  38.  /* SET UP the message port in the I/O request */
  39.     port = CreatePort (SERIALNAME,0);
  40.     IORser.IOSer.io_Message.mn_ReplyPort = port;
  41.  
  42. /*    SET PARAMS for the serial.device */
  43.     rbl = 4096;
  44.     rwl = 0x08;
  45.     wwl = 0x08;
  46.     brk = 750000;
  47.     baud= 9600;
  48.     sf  = 0x00;
  49.     t0  = 0x51040303;
  50.     t1  = 0x03030303;
  51.  
  52.   if ((error = setparams (rbl,rwl,wwl,brk,baud,sf,t0,t1)) != 0) {
  53.         printf ("bad news %ld on setup \n", error);  
  54.         exit (error);    /* redundant err msg, but who's counting ?? */
  55.     } 
  56.  
  57.     writeSer ("\n\015opened ok\n\015", -1);
  58.     writeSer ("test of -1 length\n\015", -1);
  59.     sendWaitWrite ("this is a sendio test\n\015", 23);
  60.     sendWaitWrite ("this is another sendio test\n\015", -1);
  61.     writeSer ("\n\015type 16 characters to send to amiga\n\015", -1);
  62.     actual = readSer (buffer,16);
  63.     writeSer ("\n\015Buffer was :\n\015", 16); 
  64.     writeSer (buffer, actual); 
  65.     writeSer ("\n\015end\n\015", -1); 
  66.  
  67.  /* CLOSE the serial.device */
  68.     CloseDevice (&IORser);
  69.     DeletePort (port);
  70.     exit (0);
  71. }
  72.  
  73.  /* SERIAL I/O functions */
  74.  
  75. setparams(rbuf_len,rlen,wlen,brk,baud,sf,ta0,ta1)
  76.  
  77.     unsigned long rbuf_len;
  78.     unsigned char rlen;
  79.     unsigned char wlen;
  80.     unsigned long brk;
  81.     unsigned long baud;
  82.     unsigned char sf;
  83.     unsigned long ta0;
  84.     unsigned long ta1;
  85.  
  86. {
  87.     int error;
  88.  
  89.     IORser.io_ReadLen       = rlen;
  90.     IORser.io_BrkTime       = brk;
  91.     IORser.io_Baud          = baud;
  92.     IORser.io_WriteLen      = wlen;
  93.     IORser.io_StopBits      = 0x01;
  94.     IORser.io_RBufLen       = rbuf_len;
  95.     IORser.io_SerFlags      = sf;
  96.     IORser.IOSer.io_Command = SDCMD_SETPARAMS;
  97.     IORser.io_TermArray.TermArray0 = ta0;
  98.     IORser.io_TermArray.TermArray1 = ta1;
  99.  
  100.     if ((error = DoIO (&IORser)) != 0) {
  101.         printf ("serial.device setparams error %ld \n", error);
  102.         exit (1);
  103.     }
  104.     return (error);
  105. }
  106.  
  107. readSer(data,length)
  108.     char *data;
  109.     ULONG length;
  110. {
  111.     int error;
  112.  
  113.     IORser.IOSer.io_Data = data;
  114.     IORser.IOSer.io_Length = length;
  115.     IORser.IOSer.io_Command = CMD_READ;
  116.  
  117.     if ((error = DoIO (&IORser)) != 0) {
  118.         printf ("serial.device read error %ld \n", error);
  119.         exit (1);
  120.     }
  121.     return (IORser.IOSer.io_Actual);
  122. }
  123.  
  124.  
  125. writeSer(data,length)
  126.     char *data;
  127.     int length;
  128. {
  129.     int     error;
  130.  
  131.     IORser.IOSer.io_Data = data;
  132.     IORser.IOSer.io_Length = length;
  133.     IORser.IOSer.io_Command = CMD_WRITE;
  134.  
  135.     if ((error = DoIO (&IORser)) != 0) {
  136.         printf ("serial.device write error %ld \n", error);
  137.         exit (1);
  138.     }
  139.     return (error);
  140. }
  141.  
  142. sendWaitWrite(data,length)
  143.     char *data;
  144.     int length;
  145. {
  146.     int     error;
  147.  
  148.     IORser.IOSer.io_Data = data;
  149.     IORser.IOSer.io_Length = length;
  150.     IORser.IOSer.io_Command = CMD_WRITE;
  151.  
  152.     SendIO (&IORser);
  153.  
  154.     if ((error = WaitIO (&IORser)) != 0) {
  155.         printf ("serial.device waitio error %ld \n", error);
  156.         exit (1);
  157.     }
  158.     return (IORser.IOSer.io_Actual);
  159. }
  160.  
  161.